All files / api/middlewares corsMiddleware.js

0% Statements 0/4
0% Branches 0/2
0% Functions 0/1
0% Lines 0/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                                             
const cors = require('cors');
 
// Engedélyezett origin-ek whitelist (környezeti változóból)
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',') || ['http://localhost:3000'];
 
const corsOptions = {
  origin: function (origin, callback) {
    // TODO 1: Ha nincs origin (pl. Postman, curl, backend-backend hívás), engedélyezd
    // Tipp: if (!origin) return callback(null, true);
    
    // TODO 2: Ha az origin benne van az allowedOrigins-ban, engedélyezd
    // Tipp: if (allowedOrigins.includes(origin)) return callback(null, true);
    
    // TODO 3: Egyébként tiltsd le CORS hibával
    // Tipp: callback(new Error('Not allowed by CORS'));
  },
  credentials: true, // Cookie/Auth header engedélyezése
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization']
};
 
module.exports = cors(corsOptions);